package com.lib.utils;

import android.app.Activity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/8/2
 *     desc  : 键盘相关工具类
 * </pre>
 */
public class KeyboardUtils {
    private KeyboardUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    public static void hideSoftInput(Activity activity) {
        InputMethodManager mImm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        View view = activity.getCurrentFocus();
        if (view == null) {
            view = new View(activity);
        }
        mImm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    /**
     * 动态显示软键盘
     *
     * @param edit 输入框
     */
    public static void showSoftInput(Activity activity, EditText edit) {

        InputMethodManager mImm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        edit.setFocusable(true);
        edit.setFocusableInTouchMode(true);
        edit.requestFocus();
        mImm.showSoftInput(edit, 0);
    }

    /**
     * 切换键盘显示与否状态
     */
    public static void toggleSoftInput(Activity activity) {

        InputMethodManager mImm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        mImm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    }

    /**
     * 判断键盘是否显示
     *
     * @param activity 当前activity
     * @return {@code true}: 显示<br>{@code false}: 不显示
     */
    public static boolean isShowSoftInput(Activity activity) {

        InputMethodManager mImm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        return mImm.isActive();
    }
}